{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-03T12:45:55.648784Z",
     "start_time": "2025-03-03T12:45:47.301870Z"
    }
   },
   "source": [
    "import os\n",
    "\n",
    "from sklearn.datasets import fetch_california_housing\n",
    "from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression, Lasso\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import mean_squared_error, classification_report, roc_auc_score\n",
    "import joblib\n",
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T12:49:04.854302Z",
     "start_time": "2025-03-03T12:49:04.843299Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 线性回归模型，使用加利福尼亚住房数据集\n",
    "fe_cal = fetch_california_housing(data_home='data')\n",
    "\n",
    "print(\"Colum\")\n",
    "print(fe_cal.data.shape)\n",
    "print(fe_cal.target)        # 单位：10万美金\n",
    "print(fe_cal.feature_names)"
   ],
   "id": "14aa528a0e67c8f4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Colum\n",
      "(20640, 8)\n",
      "[4.526 3.585 3.521 ... 0.923 0.847 0.894]\n",
      "['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T12:50:11.433506Z",
     "start_time": "2025-03-03T12:50:11.421576Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 分化数据集\n",
    "x_train, x_test, y_train, y_test = train_test_split(fe_cal.data, fe_cal.target, test_size=0.25, random_state=1)"
   ],
   "id": "1765438cf7e24506",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T12:51:17.821241Z",
     "start_time": "2025-03-03T12:51:17.811968Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标准化处理\n",
    "std_x = StandardScaler()\n",
    "\n",
    "x_train = std_x.fit_transform(x_train) #训练集标准化\n",
    "x_test = std_x.transform(x_test) #测试集标准化"
   ],
   "id": "c89bd37676175b65",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T12:53:39.906014Z",
     "start_time": "2025-03-03T12:53:39.902102Z"
    }
   },
   "cell_type": "code",
   "source": [
    "std_y = StandardScaler()\n",
    "y_train = std_y.fit_transform(y_train.reshape(-1, 1))\n",
    "temp = y_train.reshape(-1, 1) #-1 代表把剩余的元素都堆到哪一维\n",
    "print(temp.shape)"
   ],
   "id": "14c433da10681296",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(15480, 1)\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T12:52:24.115197Z",
     "start_time": "2025-03-03T12:52:24.111562Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看reshape -1的效果\n",
    "test1=np.array([1,2,3])\n",
    "print(test1.shape)\n",
    "print(test1.reshape(-1,1))"
   ],
   "id": "5071357ba4aca3bd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,)\n",
      "[[1]\n",
      " [2]\n",
      " [3]]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T12:56:07.831919Z",
     "start_time": "2025-03-03T12:56:07.824326Z"
    }
   },
   "cell_type": "code",
   "source": [
    "lr = LinearRegression()\n",
    "# fit是耗时的\n",
    "lr.fit(x_train, y_train)\n",
    "#回归系数可以看特征与目标之间的相关性\n",
    "print('回归系数', lr.coef_)\n",
    "#\n",
    "y_predict = lr.predict(x_test)\n",
    "\n",
    "# 预测测试集的房子价格，通过inverse得到真正的房子价格\n",
    "y_lr_predict = std_y.inverse_transform(y_predict)\n",
    "\n",
    "print(\"正规方程测试集里面每个房子的预测价格：\", y_lr_predict[0:10])\n",
    "print(\"正规方程的均方误差：\", mean_squared_error(y_test, y_lr_predict))"
   ],
   "id": "a8242311c59cd01b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "回归系数 [[ 0.71942632  0.10518431 -0.23147194  0.26802332 -0.00448136 -0.03495117\n",
      "  -0.7849086  -0.76307353]]\n",
      "正规方程测试集里面每个房子的预测价格： [[2.12391852]\n",
      " [0.93825754]\n",
      " [2.7088455 ]\n",
      " [1.70873764]\n",
      " [2.82954754]\n",
      " [3.50376456]\n",
      " [3.0147162 ]\n",
      " [1.62781292]\n",
      " [1.74317518]\n",
      " [2.01897806]]\n",
      "正规方程的均方误差： 0.5356532845422556\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "正规方程的均方误差： 0.5356532845422556",
   "id": "c439ced5dfd4bbbb"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "1a021ec4bf64587f"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
